package com.dmz.spring.security.authentication.sms;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * @Title: SmsCodeAuthenticationFilter
 * @ProjectName spring-security-main
 * @date 2020/12/211:24
 */
public class SmsCodeAuthenticationFilter extends
        AbstractAuthenticationProcessingFilter {
    // ~ Static fields/initializers
    // =====================================================================================
 
    public static final String IMOOC_FORM_MOBILE_KEY = "mobile";
 
    private String mobileParameter = IMOOC_FORM_MOBILE_KEY;
    private boolean postOnly = true;
 
    // ~ Constructors
    // ===================================================================================================
 
    public SmsCodeAuthenticationFilter() {
        super(new AntPathRequestMatcher("/authentication/mobile", "POST"));
    }
 
    // ~ Methods
    // ========================================================================================================
 
    public Authentication attemptAuthentication(HttpServletRequest request,
                                                HttpServletResponse response) throws AuthenticationException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }
 
        String mobile = obtainMobile(request);
 
 
        if (mobile == null) {
            mobile = "";
        }
 
        mobile = mobile.trim();
 
        SmsCodeAuthenticationToken authRequest = new SmsCodeAuthenticationToken(mobile);
 
        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);
 
        return this.getAuthenticationManager().authenticate(authRequest);
    }
 
    protected String obtainMobile(HttpServletRequest request) {
        return request.getParameter(mobileParameter);
    }
 
    protected void setDetails(HttpServletRequest request,
                              SmsCodeAuthenticationToken authRequest) {
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }
 
    public void setPostOnly(boolean postOnly) {
        this.postOnly = postOnly;
    }
}